#include <Megabrite.h>
#include <MegabriteController.h>
#include <math.h>
#include <avr/pgmspace.h>

#define clockpin 13
#define enablepin 10
#define latchpin 9
#define datapin 11

float electret = 0;
float electret2 = 0;
float val = 0;
int count = 0;
int min = 0;
Megabrite megabrite;
MegabriteController megabriteController;

void setup() {
	Serial.begin(9600);
	megabriteController.setDataPin(11);;
	megabriteController.setLatchPin(9);
	megabriteController.setEnablePin(10);
	megabriteController.setClockPin(13);
	megabriteController.addMegabrite(&megabrite);
	megabriteController.init();
	megabrite.red(1023);
	megabrite.green(min);
	megabrite.blue(min);
}

float readVals(int pin) {
	float electret = analogRead(pin);
	if (electret < 0) {
		electret = 0;
	}

	return electret;
}

void loop() {
	electret = readVals(0);
	electret2 = readVals(1);

	if (electret2 > 600 || electret2 > 600) {
		Serial.print(electret);
		Serial.print("/");
		Serial.println(electret2);
	}

	electret = electret + electret2 - 1023;
	if (electret < 100) {
		electret = 0;
	}

	if (electret > megabrite.blue()) {
		megabrite.blue(electret);
		megabrite.green(electret);
	} else {
		val = megabrite.blue() - 2;
		if (val < min) {
			val = min;
		}
		megabrite.blue(val);
		megabrite.green(val);
	}

	megabriteController.send();
	delay(20);
}

